/**
 * 获得搜索参数
 * @param params
 * @returns {Object}
 */
function getSearchParams(params) {
    var searchParams = new Object();
    if (params != undefined) {
        searchParams = params;
    }
    $('.SearchForm').each(function() {
        var param = $(this).val().trim();
        var val = $("input:radio[name='sex']:checked").val();
        if (val == 0) {
            searchParams['sex'] = 0;
        } else if (val == 1) {
            searchParams['sex'] = 1;
        }
        if (param == undefined)
            param = '';
        searchParams[$(this).attr('id')] = param;
    });
    return searchParams;
}
//清空搜索条件
$("#Reset").click(function() {
    $('.SearchForm').val('');
    $("input:radio[name='sex']").removeAttr('checked');
});
/**
 * 页面自加载
 */
$(function() {
    // $('#student_createtime').datepicker({
    //     format: "yyyy-mm-dd",
    //     todayBtn: "linked",
    //     autoclose: true,
    //     todayHighlight: true,
    //     clearBtn: true
    // });

    var cellwidth = ($(".box-content.table-responsive").width() - 55) / 11;
    var psval = $('#datatable_homeworkinfo').attr('data-size');
    if (psval == undefined || psval == "") {
        psval = 10;
    }
    /**
     * easyui 生成表格数据
     */
    $('#datatable_homeworkinfo').datagrid({
        singleSelect: true, //允许选择多行
        striped: true,
        idField: 'h_id',
        remoteSort: false,
        collapsible: true,
        fit: false,
        url: 'admin/Student/gethomework', //数据源路径
        loadMsg: '请等待数据载入....',
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        pageSize: psval,
        pageList: [psval, psval * 2, psval * 3, psval * 4, psval * 5],
        columns: [[{
                    field: 'stu_nickname',
                    title: '学生姓名',
                    align: 'center',
                    width: cellwidth,
                    sortable: true
                }, {
                    field: 's_name',
                    title: '课程',
                    align: 'center',
                    width: cellwidth,
                    sortable: true
                }, {
                    field: 'tea_name',
                    title: '老师',
                    align: 'center',
                    width: cellwidth,
                    sortable: true
                }, {
                    field: 'h_date',
                    title: '预约日期',
                    align: 'center',
                    width: cellwidth,
                    sortable: true
                }, {
                    field: 'h_status',
                    title: '预约状态',
                    align: 'center',
                    width: cellwidth,
                    sortable: true,
                    formatter:function(value,row,index){
                        if(value==0){
                            return "<span style='color: green;'>可以预约</span>";
                        }
                        if(value==1){
                            return "<span style='color: red;'>已预约</span>";
                        }
                        if(value==2){
                            return "<span style='color: red;'>已上传(未批改)</span>";
                        }
                        if(value==3){
                            return "<span style='color: red;'>已批改</span>";
                        }
                    }
                }
                // ,{
                //     field: 'h_id',
                //     title: '操作',
                //     align: 'center',
                //     width: cellwidth,
                //     sortable: true,
                //     formatter:function(value,row,index){
                //         if(row.h_status == 2){
                //             return "<a onclick='seeStuUrl(this);' name='"+value+"' href='JavaScript:;'>查看作业</a>";
                //         }
                //         if(row.h_status == 3){
                //             return "<a onclick='seeTeaUrl(this);' name='"+value+"' href='JavaScript:;'>查看批改 | </a>"+
                //                 "<a onclick='seeStuUrl(this);' name='"+value+"' href='JavaScript:;'>查看作业</a>";
                //         }
                //         return '无';
                //
                //     }
                // }
                ]],
        onBeforeLoad: function(param) {
            param = getSearchParams(param);
        },
    });


    $('#Search').click(function() {
        $('#datatable_homeworkinfo').datagrid('reload');
    })


    /**
     * 删除（待定）
     */
    $('.removebook').click(function() {
        var row = $('#datatable_homeworkinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一行数据！',
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '只能选择一行！',
            });
            return;
        }
        if(row[0].h_status == 0){
            $.TeachDialog({
                content: '没有预约，不用取消！',
            });
            return;
        }
        $.TeachDialog({
            content: "操作不可逆，确认删除此预约？",
            showCloseButton: true,
            showCloseButtonName: '取消',
            otherButtons: ['确认'],
            otherButtonStyles: ['btn-danger'],
            clickButton: function(sender, modal, index) {
                modal.modal('hide');
                $.ajax({
                    url: 'admin/Student/deleteHomeworkByID', //form action
                    dataType: 'JSON', //返回体类型
                    type: 'POST', // form type
                    data: "hid=" + row[0].h_id,
                    success: function(data) {
                        if (data.status == 200) {
                            $.TeachDialog({
                                content: data.msg,
                            });
                            $('#datatable_homeworkinfo').datagrid('reload');
                            $('#datatable_homeworkinfo').datagrid('uncheckAll');
                        } else {
                            $.TeachDialog({
                                content: '删除失败',
                            });
                        }
                    },
                    error: function() {
                        $.TeachDialog({
                            content: '删除失败',
                        });
                    }
                });
            }
        })
    });


    /**
     * 编辑
     */
    $(".editbook").click(function() {
        var row = $('#datatable_homeworkinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一行数据！'
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '只能选择一行数据！'
            });
            return;
        }
        //获得修改框内容
        var content = "";
        $.ajax({
            url: 'admin/Student/updatehomeworkmodal',
            data: "hid=" + row[0].h_id,
            type: 'POST',
            dataType: 'HTML', //返回的数据类型
            success: function(updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    modalId: null,
                    animation: null,
                    title: '修改预约信息',
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    CloseButtonAddFunc: function() {
                    },
                    otherButtons: ['确定'],
                    clickButton: function(sender, modal, index) {
                        if(content == '以前的记录不能修改'){
                            modal.modal('hide');
                            return;
                        }
                        $.ajax({
                            url: 'admin/Student/updatehomework',
                            data: $("#updateForm").serialize(),
                            type: 'POST',
                            dataType: 'JSON',
                            success: function(returnData) {
                                if (returnData.status == 0) {
                                    $.TeachDialog({
                                        content: returnData.msg,
                                        showCloseButton: false,
                                        CloseButtonAddFunc: function() {

                                        }
                                    });
                                } else {
                                    modal.modal('hide');
                                    $.TeachDialog({
                                        content: returnData.msg,
                                        showCloseButton: false,
                                    });
                                    $('#datatable_homeworkinfo').datagrid('reload');
                                }
                            },
                            error: function() {
                                $.TeachDialog({
                                    content: '修改失败',
                                });
                            },
                        });
                    },
                });
            },
            error: function() {
                $.TeachDialog({
                    content: '获取数据失败，无法进行修改',
                });
                return;
            }
        });

    });
    /**
     * 查看学生详情
     */
    $(".addbook").click(function() {
        var row = $('#datatable_homeworkinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一个学生进行查看！'
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '请选择一个学生进行查看！',
            });
            return;
        }
        //获得修改框内容
        var content = "";
        $.ajax({
            url:"admin/Student/seeinfo",
            data:"stu_id=" + row[0].stu_id,
            type:'POST',
            dataType:'HTML',
            success:function(returndata){
                content = returndata;
                $.TeachDialog({
                    modalId: null,
                    animation: null,
//                        largeSize:true,
                    title: "<h2>学生详情</h2>",
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    CloseButtonAddFunc: function () {
                    }
                });
            },
            error:function(){
                alert('数据返回出错，无伤大雅，请继续');
            }
        });

    });
});
